package com.hualan.controller;

import com.hualan.domain.Heartmark;
import com.hualan.service.HeartmarkService;
import com.hualan.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/HeartmarkController")
public class HeartmarkController {
    @Autowired
    private HeartmarkService heartmarkService;

    @GetMapping("/selectAll")
    public R selecAll(){
        return heartmarkService.selectAll();
    }


    @GetMapping("/selectAllByPage")
    public R selAllFreePage(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,@RequestParam(name = "pageSize",defaultValue = "5")int pageSize){
        return R.success(heartmarkService.selAllByPage(pageNum,pageSize));
    }

    @PostMapping("/add")
    public R addHeartMark(Heartmark heartmark){
        return heartmarkService.addHeartMark(heartmark);
    }


    @PostMapping("/updateById")
    public R updateById(Heartmark heartmark){
        return heartmarkService.updateById(heartmark);
    }

    @DeleteMapping("/deleteById")
    public R deleteById(int id){
        return heartmarkService.delectById(id);
    }

    /*
     * 在springmvc中通过MultipartFile对象来接收上传的文件
     * MultipartFile对象就是通过文件上传解析器创建的
     */
        @PostMapping("/upload")
        public R upload(MultipartFile file, HttpSession httpSession) throws Exception{
// 获取上传文件的名称
        String filename = file.getOriginalFilename();// aa.jpg
        // 处理文件重名问题: 截取文件扩展名，用UUID替代文件名
        filename = UUID.randomUUID().toString()+filename;

// 获取服务器中upload目录的路径

        ServletContext servletContext= httpSession.getServletContext();
        String rootPath = servletContext.getRealPath("upload"); // 获取upload文件夹的真实路径
// 当前日期文件夹

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//        String dateDirPath = sdf.format(new Date());
//        File dfile = new File(rootPath+ File.separator + dateDirPath);
//        if (!dfile.exists()) {
//            dfile.mkdirs();
//        }
// 文件上传的最终路径
        String finalPath = rootPath + File.separator+ filename;
        System.out.println("文件上传的最终路径:" + finalPath);
//实现上传功能
        file.transferTo(new File(finalPath));
        return R.success(filename);
    }
    @GetMapping("/down")
    public void down(String filename, HttpServletResponse response, HttpSession httpSession) throws IOException {
        //获取根路径
        ServletContext servletContext = httpSession.getServletContext();
        String rootPath = servletContext.getRealPath("upload");

        //最终路径

        String finalPath = rootPath + File.separator + filename;
        System.out.println(finalPath);
        FileInputStream inputStream = new FileInputStream(finalPath);
        ServletOutputStream outputStream = response.getOutputStream();

        byte[] b = new byte[1024];
        int i = inputStream.read(b);
        while (i != 1){
            outputStream.write(b, 0, i);
            i = inputStream.read(b);
        }
        outputStream.close();
        inputStream.close();
    }
    @GetMapping("/getAllHeartmark")
    public R getCePingMessage() {
        return heartmarkService.queryAllCePing();
    }
}
